package com.guofen.controller.sjq;

import com.guofen.pojo.SjqWeightProductionKitchenInfo;
import com.guofen.pojo.SjqWeightProductionKitchenInfo;
import com.guofen.service.sjq.SjqWeightProductionKitchenInfoService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RequestMapping("/sjq/weightProductionKitchenInfo")
@Controller
@Api(tags = "SjqWeightProductionKitchenInfoController", description = "称重数据导入表-厨余")
public class SjqWeightProductionKitchenInfoController {

    @Autowired
    SjqWeightProductionKitchenInfoService sjqWeightProductionKitchenInfoService;

    @ApiOperation("EXCEL批量添加记录")
    @PostMapping("/addFromKitchenExcel")
    @ResponseBody
    public Result doRegisterFromExcel1(@RequestParam("file") MultipartFile file){
        String EXCEL_XLS = "xls";
        String EXCEL_XLSX = "xlsx";

        if (file.isEmpty())
            return ResultGenerator.genFailResult("文件为空");
        List<Boolean> registerResult = new ArrayList<>();

        try {
            //根据路径获取这个操作excel的实例
            Sheet sheet = null;
            Row row = null;
            if (file.getOriginalFilename().endsWith(EXCEL_XLS)) {
                HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
                sheet = wb.getSheetAt(0);
            } else if (file.getOriginalFilename().endsWith(EXCEL_XLSX)) {
                XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
                //根据页面index 获取sheet页
                sheet = wb.getSheetAt(0);
            }

            List<SjqWeightProductionKitchenInfo> sjqWeightProductionKitchenInfoList= new ArrayList<>();

            for (int i = 2; i < Objects.requireNonNull(sheet).getPhysicalNumberOfRows(); i++) {
                //获取每一行数据
                row = sheet.getRow(i);
                if(row.getCell(0)==null && row.getCell(1)==null && row.getCell(2)==null && row.getCell(3)==null
                        && row.getCell(4)==null && row.getCell(5)==null && row.getCell(6)==null
                        && row.getCell(7)==null && row.getCell(8)==null){
                    registerResult.add(false);
                    continue;
                }

                String id = row.getCell(0).getStringCellValue();
                if(!id.equals("合计") && !id.equals("总合计")){
                    SjqWeightProductionKitchenInfo sjqWeightProductionKitchenInfo = new SjqWeightProductionKitchenInfo();
                    if (row.getCell(1) != null){
                        Cell cell = row.getCell(1);
                        cell.setCellType(CellType.STRING);
                        sjqWeightProductionKitchenInfo.setLicensePlate(cell.getStringCellValue());
                    }
                    if (row.getCell(2) != null){
                        Cell cell = row.getCell(2);
                        cell.setCellType(CellType.STRING);
                        sjqWeightProductionKitchenInfo.setCarryCompany(cell.getStringCellValue());
                    }
                    if (row.getCell(3) != null){
                        Cell cell = row.getCell(3);
                        cell.setCellType(CellType.STRING);
                        sjqWeightProductionKitchenInfo.setGarbageType(cell.getStringCellValue());
                    }
                    if (row.getCell(4) != null){
                        Cell cell = row.getCell(4);
                        cell.setCellType(CellType.STRING);
                        sjqWeightProductionKitchenInfo.setCarDoorNumber(cell.getStringCellValue());
                    }
                    if (row.getCell(5) != null){
                        Cell cell = row.getCell(5);
                        cell.setCellType(CellType.STRING);
                        sjqWeightProductionKitchenInfo.setGrossWeight(Double.parseDouble(cell.getStringCellValue()));
                    }
                    if (row.getCell(6) != null){
                        Cell cell = row.getCell(6);
                        cell.setCellType(CellType.STRING);
                        sjqWeightProductionKitchenInfo.setTareWeight(Double.parseDouble(cell.getStringCellValue()));
                    }
                    if (row.getCell(7) != null){
                        Cell cell = row.getCell(7);
                        cell.setCellType(CellType.STRING);
                        sjqWeightProductionKitchenInfo.setNetWeight(Double.parseDouble(cell.getStringCellValue()));
                    }
                    if (row.getCell(8) != null){
                        Cell cell = row.getCell(8);
                        cell.setCellType(CellType.STRING);
                        sjqWeightProductionKitchenInfo.setNetWeightTime(cell.getStringCellValue());
                    }

                    sjqWeightProductionKitchenInfoList.add(sjqWeightProductionKitchenInfo);
                }
            }
            registerResult = sjqWeightProductionKitchenInfoService.addFromKitchenExcel(sjqWeightProductionKitchenInfoList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultGenerator.genSuccessResult(registerResult);
    }

    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(sjqWeightProductionKitchenInfoService.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam(required = false) Integer id,
                         @ApiParam(name="licensePlate",value="车牌号码",required=false) @RequestParam(required = false) String licensePlate,
                         @ApiParam(name="carryCompany",value="承运单位",required=false) @RequestParam(required = false) String carryCompany,
                         @ApiParam(name="garbageType",value="垃圾类型",required=false) @RequestParam(required = false) String garbageType,
                         @ApiParam(name="carDoorNumber",value="车门号",required=false) @RequestParam(required = false) String carDoorNumber,
                         @ApiParam(name="grossWeight",value="毛重(KG)",required=false) @RequestParam(required = false) Double grossWeight,
                         @ApiParam(name="tareWeight",value="皮重(KG)",required=false) @RequestParam(required = false) Double tareWeight,
                         @ApiParam(name="netWeight",value="净重(KG)",required=false) @RequestParam(required = false) Double netWeight,
                         @ApiParam(name="netWeightTime",value="净重时间",required=false) @RequestParam(required = false) String netWeightTime) {
        SjqWeightProductionKitchenInfo record = new SjqWeightProductionKitchenInfo();
        record.setId(id);
        record.setLicensePlate(licensePlate);
        record.setCarryCompany(carryCompany);
        record.setGarbageType(garbageType);
        record.setCarDoorNumber(carDoorNumber);
        record.setGrossWeight(grossWeight);
        record.setTareWeight(tareWeight);
        record.setNetWeight(netWeight);
        record.setNetWeightTime(netWeightTime);
        return ResultGenerator.genSuccessResult(sjqWeightProductionKitchenInfoService.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam(required = true) Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam(required = true) Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间",required = false) @RequestParam(required = false) Long startTime,
                         @ApiParam(name="endTime",value="截止时间",required = false) @RequestParam(required = false) Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(sjqWeightProductionKitchenInfoService.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(sjqWeightProductionKitchenInfoService.getByTime(pageSize, currentPage, startTime, endTime));
    }
}
